﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class modifyPass : BasePage
    {
        WTODataContext contex = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindGrid();
                GetInfo();

            }
        }
        private void GetInfo()
        {
            admin cat = contex.admins.SingleOrDefault(p => p.admin_login_name == Session["login_name"].ToString());
            if (cat != null)
            {
                this.txtLoginName.Text = cat.admin_login_name;
                this.txtLoginName.Enabled = false;
                this.txtName.Text = cat.admin_name;
                this.txtCellphone.Text = cat.admin_mobile.ToString();
                this.txtTelephone.Text = cat.admin_telephone.ToString();
                this.txtAddress.Text = cat.admin_address;
                if (cat.admin_type != null)
                {
                    this.ddlType.Items.FindByValue(cat.admin_type.ToString()).Selected = true;
                    this.ddlType.Enabled = false;
                }

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
            }
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            bool IsModifyPassword=false;
            //此种条件下管理员想修改密码
            if (this.txtOldPass.Text.Trim() != "" || this.txtNewPass.Text.Trim() != "" || this.txtNewPass2.Text.Trim() != "")
            {
                if (this.txtOldPass.Text.Trim() == "")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "oldpasserr", "alert('请输入旧密码');", true);
                    return;
                }
                if (this.txtNewPass.Text.Trim() == "")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "newpasserr", "alert('请输入新密码');", true);
                    return;
                }
                if (this.txtNewPass2.Text.Trim() == "")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "newpasserr2", "alert('请输入确认新密码');", true);
                    return;
                }
                if (this.txtNewPass.Text.Trim() != this.txtNewPass2.Text.Trim())
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "passwordmatch", "alert('两次密码不一致');", true);
                    return;
                }
                IsModifyPassword=true;
            }
            admin cat = contex.admins.SingleOrDefault(p => p.admin_login_name == Session["login_name"].ToString());
            if (cat != null)
            {
                if (IsModifyPassword)
                {
                    if (this.txtOldPass.Text.Trim() != cat.admin_password)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "passwordmatch", "alert('您输入的旧密码不正确');", true);
                        return;
                    }
                    cat.admin_password = this.txtNewPass.Text.Trim();
                }
                cat.admin_mobile = this.txtCellphone.Text.Trim();
                cat.admin_name = this.txtName.Text.Trim();
                cat.admin_telephone = this.txtTelephone.Text.Trim();
                cat.update_date = DateTime.Now;
                cat.admin_address = this.txtAddress.Text.Trim();
                contex.SubmitChanges();
                ClientScript.RegisterStartupScript(this.GetType(), "sueecss", "alert('修改个人信息成功(如果修改密码，将会在下次登录时生效)');", true);
            }

        }
    }
}